﻿@*
Available model properties:
    CartItems
        Product
        Title
        Sku
        ProductImage
        IsDigital
        Price
        Weight
        ShippingCost
    ShippingMethods
        Price
        DisplayName
        Name
        ShippingCompany
        IncludedShippingAreas
        ExcludedShippingAreas
    Currency
    WeightUnit
    MerchantId
    AnalyticsId
    UseSandbox
    Custom
*@
@{
    Script.Require("jQuery").AtFoot();
    
    var i = 0;
    var isMini = Model.IsMiniCart != null && (bool)Model.IsMiniCart;
    var urlHelper = new UrlHelper(Request.RequestContext);
    var baseUrl = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, urlHelper.Content("~"));
    // using jQuery to get the actual page path for the return url since it is too hard in a partial view to get the current page path
    
    // If any products are digital, only want to show PayPal icon to authenticated users
    // If no products are digital, PayPal icon can be shown to anyone
    bool anyDigital = false;
    foreach (var product in Model.CartItems) {
        if (product.IsDigital) {
            anyDigital = true;
        }   
    }
    var isUserAuthenticated = User.Identity.IsAuthenticated;
    
    bool showPayPal = false;
    if (isUserAuthenticated) {
        showPayPal = true;
    }
    else if (!anyDigital) {
        showPayPal = true;
    }
    else if (anyDigital && isUserAuthenticated) {
        showPayPal = true;   
    }
}

@if (showPayPal) {
<form method="post" action="@Model.PayPalCheckoutURL">
    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="upload" value="1" /> @* Value of 1 to pass all the rows to PayPal *@
    <input type="hidden" name="business" value="@Model.BusinessEmailAddress" />
    <input type="hidden" name="currency_code" value="@Model.Currency" />
    @foreach (var product in Model.CartItems) {
        i++;
        <input type="hidden" name="@("item_number_" + i)" value="@product.Product.Id" />
        <input type="hidden" name="@("item_name_" + i)" value="@product.Title" /> 
        <input type="hidden" name="@("quantity_" + i)" value="@product.Quantity" />
        <input type="hidden" name="@("amount_" + i)" value="@product.Price" />
    }
    
    @* Need to implement ship by weight - https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_cart_upload *@
    
    <input type="hidden" name="return" value="@string.Format("{0}{1}", baseUrl, "PayPal/Returned")" />
    <input type="hidden" name="cancel_return" value="@string.Format("{0}{1}", baseUrl, "PayPal/Canceled")" />
    <input type="hidden" name="notify_url" value="@string.Format("{0}{1}", baseUrl, "PayPal/HandleIPN")" />
    
    @{
        var custom = ((Model.Custom as IEnumerable<string>) ?? new string[] { }).ToList();
        if (custom.Any()) {
            <input type="hidden" name="custom" value="@string.Join("; ", custom)"/>
        }
        i = 0;
    }
    
    @{
        var buttonUrl = String.Format(Model.IsSandbox ?
            "/Modules/Rework.CommerceHistory/Content/Images/PayPalUploadCompleteCart.gif" :
            "/Modules/Rework.CommerceHistory/Content/Images/PayPalUploadCompleteCart.gif",
            Model.MerchantId);
        var width = isMini ? 160 : 180;
        var height = isMini ? 43 : 46;
    }

    <input type="image" name="PayPal Checkout" alt="@T("Fast checkout through PayPal")"
           src="@(buttonUrl)" height="@height" width="@width"/>
</form>
}
else {
    <p>
        <a id="payPalSignInUrl" href="/Users/Account/LogOn">Sign In</a> to purchase digital items with PayPal.
    </p>   
    
    <script type="text/javascript">
        $(document).ready(function () {
            var pathname = window.location.pathname;
            $("#payPalSignInUrl").attr("href", "/users/Account/Logon?ReturnUrl=" + pathname);
        });
    </script> 
}
